module V1
  module Albums
    extend ActiveSupport::Concern

    include CorsHelpers

    included do
      resource :albums do
        cors '/', origins: ['http://localhost:9000'], methods: %w(GET POST)
        cors '/:id', origins: ['http://localhost:9000'], methods: %w(GET PATCH DELETE)

        get '/' do
          Album.page(params[:page]).per(params[:per]).select([:id, :name])
        end

        post '/' do
          authenticate_user!
          album = Album.create(params[:album])
          { id: album.id }
        end

        get '/:id' do
          Album.where(id: params[:id]).first || error!('Not Found', 404)
        end
      end
    end
  end
end